package com.vdimitr.happycube.puzzle.solution.output.conversion;

import com.vdimitr.happycube.puzzle.piece.PuzzlePiece;
import com.vdimitr.happycube.puzzle.solution.output.translation.IPuzzlePieceMatrixValueTranslator;

/**
 * This class is responsible for converting the matrix representation of a
 * puzzle piece to a matrix of strings
 * 
 * @see IPuzzlePieceMatrixValueTranslator
 * 
 * @author vasileios.dimitriadis
 * 
 */
public class PuzzlePieceMatrixRowToStringConverter {

	/**
	 * The translator to use for the values of the matrix representation of a
	 * puzzle piece
	 */
	private IPuzzlePieceMatrixValueTranslator translator;

	/**
	 * Constructor
	 * 
	 * @param translator
	 *            to be used for translating the values of a puzzle piece's
	 *            matrix to strings
	 */
	public PuzzlePieceMatrixRowToStringConverter(
			IPuzzlePieceMatrixValueTranslator translator) {
		this.translator = translator;
	}

	/**
	 * Converts the matrix representation of a puzzle piece to a matrix of
	 * strings
	 * 
	 * @param puzzlePiece
	 *            to be converted
	 * @return a matrix of strings, where each row represents the translated and
	 *         concatenated row of the given puzzle piece matrix
	 */
	public String[] toString(PuzzlePiece puzzlePiece) {
		int puzzlePieceMatrixLength = puzzlePiece.getMatrix().length;
		String[] puzzlePieceMatrixToString = new String[puzzlePieceMatrixLength];
		for (int i = 0; i < puzzlePieceMatrixLength; i++) {
			puzzlePieceMatrixToString[i] = toString(puzzlePiece.getMatrix()[i]);
		}
		return puzzlePieceMatrixToString;
	}

	/**
	 * Translates and concatenates a row of the given puzzle piece matrix
	 * 
	 * @param puzzlePieceMatrixRow
	 *            a row of the matrix representation of puzzle piece
	 * @return a string representing the given row
	 */
	private String toString(int[] puzzlePieceMatrixRow) {
		StringBuilder sb = new StringBuilder(puzzlePieceMatrixRow.length);
		for (int i = 0; i < puzzlePieceMatrixRow.length; i++) {
			sb.append(translator.translateToString(puzzlePieceMatrixRow[i]));
		}
		return sb.toString();
	}

}
